package jp.seraph.same.model;

import java.util.List;

public abstract class AbstractHierarchicalViewModel<ChildType extends ViewModel> extends AbstractViewModel implements HierarchicalViewModel<ChildType> {

    protected List<ChildAddedListener<ChildType>> mAddedListeners;
    protected List<ChildRemovedListener<ChildType>> mRemovedListeners;

    public AbstractHierarchicalViewModel(String aName) {
        super(aName);
    }

    /**
     * @see jp.seraph.same.model.HierarchicalViewModel#addAddedListenner(jp.seraph.same.model.ChildAddedListener)
     */
    public void addAddedListenner(ChildAddedListener<ChildType> aListener) {
        mAddedListeners.add(aListener);
    }

    /**
     * @see jp.seraph.same.model.HierarchicalViewModel#removeAddedListener(jp.seraph.same.model.ChildAddedListener)
     */
    public void removeAddedListener(ChildAddedListener<ChildType> aListener) {
        mAddedListeners.remove(aListener);
    }

    protected void fireAddedEvent(ChildType aNewChild) {
        for (ChildAddedListener<ChildType> tListener : mAddedListeners) {
            tListener.handle(this, aNewChild);
        }
    }

    /**
     * @see jp.seraph.same.model.HierarchicalViewModel#addRemovedListener(jp.seraph.same.model.ChildRemovedListener)
     */
    public void addRemovedListener(ChildRemovedListener<ChildType> aListener) {
        mRemovedListeners.add(aListener);
    }

    /**
     * @see jp.seraph.same.model.HierarchicalViewModel#removeRemovedListener(jp.seraph.same.model.ChildRemovedListener)
     */
    public void removeRemovedListener(ChildRemovedListener<ChildType> aListener) {
        mRemovedListeners.remove(aListener);
    }

    protected void fireRemovedEvent(ChildType aRemovedChild) {
        for (ChildRemovedListener<ChildType> tListener : mRemovedListeners) {
            tListener.handle(this, aRemovedChild);
        }
    }

}